package mo.leetcode_top;

import java.util.Random;

public class L528 {

    private Random random = new Random();
    private int[] w;
    private int length = 0;
    private int sum = 0;

    public L528(int[] w) {
        this.w = w;
        for (int i = 0; i < w.length; i++) {
            sum += w[i];
            length = w.length;
        }
    }

    public int pickIndex() {
        int value = random.nextInt(sum) + 1;
        for (int i = 0; i < length; i++) {
            value -= w[i];
            if (value <= 0) {
                return i;
            }
        }
        return length - 1;
    }

    public static void main(String[] args) {
        L528 test = new L528(new int[]{1, 3});
        for (int i = 0; i < 10; i++) {
            System.out.println(test.pickIndex());
        }
    }

}
